﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace System.Web
{
    public class AshxFileData
    {
        public static System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("<%@\\s+WebHandler\\s+Language\\s*=\\s*\"([^\"]+)\"\\s+(CodeBehind|CodeFile)\\s*=\\s*\"([^\"]+)\"\\s+Class\\s*=\\s*\"([^\"]+)\"\\s+%>");

        public AshxFileData(string ashxFileName)
        {
            string ashxFileContent = File.ReadAllText(ashxFileName);
            var mat = reg.Match(ashxFileContent);
            if (mat.Success)
            {
                this.Language = mat.Groups[1].Value;
                if (mat.Groups[2].Value == "CodeBehind")
                {
                    this.CodeBehind = mat.Groups[3].Value;
                }
                if (mat.Groups[2].Value == "CodeFile")
                {
                    this.CodeFile = mat.Groups[3].Value;
                }
                this.Class = mat.Groups[4].Value;
            }
            else
            {
                throw new FileLoadException($"{ashxFileName}内容无法解析！");
            }
        }
        public string Language { get; set; }
        public string CodeBehind { get; set; }
        public string CodeFile { get; set; }
        public string Class { get; set; }
    }
}
